<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */

include( '../global/initialize.php' );
global $dal;

$query = "SELECT admin FROM users WHERE user_id = " . $_SESSION[ 'user_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if( isset( $row[ 'admin' ] ) && $row[ 'admin' ] == 1 ) {
	$scouts = getPostValue( 'scouts' );
	if( !is_numeric( $scouts ) ) {
		$scouts = 0;
	}
	$infantry = getPostValue( 'infantry' );
	if( !is_numeric( $infantry ) ) {
		$infantry = 0;
	}
	$cavalry = getPostValue( 'cavalry' );
	if( !is_numeric( $cavalry ) ) {
		$cavalry = 0;
	}
	$artillery = getPostValue( 'artillery' );
	if( !is_numeric( $artillery ) ) {
		$artillery = 0;
	}
	$locationId = getPostValue( 'location_id' );
	if( !isset( $locationId ) || $locationId == -1 ) {
		$x = getPostValue( 'x' );
		$y = getPostValue( 'y' );
	}
	else {
		$statement = $dal->prepare( 'SELECT x, y ' .
				'FROM locations ' .
				'WHERE location_id = :location_id' );
		$statement->bindParam( ':location_id', $locationId, PDO::PARAM_INT );
		$location = $dal->getObject( $statement );
		$x = $location->x;
		$y = $location->y;
	}
	
	if( is_numeric( $x ) && is_numeric( $y ) ) {
		$count = 0;
		$count += createArmy( getPostValue( 'ruler1' ), $x, $y, $scouts, $infantry, $cavalry, $artillery ) ? 1 : 0;
		$count += createArmy( getPostValue( 'ruler2' ), $x, $y, $scouts, $infantry, $cavalry, $artillery ) ? 1 : 0;
		$count += createArmy( getPostValue( 'ruler3' ), $x, $y, $scouts, $infantry, $cavalry, $artillery ) ? 1 : 0;
		if( $count == 0 ) {
			$_SESSION[ 'debug_army_status' ] = 'No valid ruler names found';
		}
		else {
			$_SESSION[ 'debug_army_status' ] = 'Added ' . $count . ' armies.';
		}
	}
	else {
		$_SESSION[ 'debug_army_status' ] = 'No valid input';
	}
	header( "Location: debug.php" );
}
else {
	$_SESSION[ 'admin' ] = 0;
	header( "Location: ../overview/overview.php" );
}

function createArmy( $ruler, $x, $y, $scouts, $infantry, $cavalry, $artillery ) {
	global $dal;
	$statement = $dal->prepare( 'SELECT ruler_id ' .
			'FROM ruler ' .
			'WHERE name = :name' );
	$statement->bindParam( ':name', $ruler, PDO::PARAM_STR );
	$rulerId = $dal->getValue( $statement );
	if( isset( $rulerId ) ) {
		$officerName = 'DEBUG OFFICER';
		$armyName = 'DEBUG ARMY';
		$officerNameCheck = $officerName . '%';
		$statement = $dal->prepare( 'SELECT count(*) ' .
				'FROM officers ' .
				'WHERE name LIKE :name' );
		$statement->bindParam( ':name', $officerNameCheck, PDO::PARAM_STR );
		$count = $dal->getValue( $statement ) + 1;
		$officerName .= ' ' . $count;
		$armyName .= ' ' . $count;
		
		$rank = 7;
		$ranking = 5;
		$statement = $dal->prepare( 'INSERT INTO officers ' .
				'( `name`, `ruler_id`, `rank`, `ranking` ) ' .
				'VALUES( :name, :ruler_id, :rank, :ranking )' );
		$statement->bindParam( ':name', $officerName, PDO::PARAM_STR );
		$statement->bindParam( ':ruler_id', $rulerId, PDO::PARAM_INT );
		$statement->bindParam( ':rank', $rank, PDO::PARAM_INT );
		$statement->bindParam( ':ranking', $ranking, PDO::PARAM_INT );
		$dal->execute( $statement );
		
		$statement = $dal->prepare( 'SELECT officer_id ' .
				'FROM officers ' .
				'WHERE `name` = :name AND `ruler_id` = :ruler_id AND `rank` = :rank AND `ranking` = :ranking' );
		$statement->bindParam( ':name', $officerName, PDO::PARAM_STR );
		$statement->bindParam( ':ruler_id', $rulerId, PDO::PARAM_INT );
		$statement->bindParam( ':rank', $rank, PDO::PARAM_INT );
		$statement->bindParam( ':ranking', $ranking, PDO::PARAM_INT );
		$officerId = $dal->getValue( $statement );
		
		$statement = $dal->prepare( 'INSERT INTO army ' .
				'( `name`, `officer_id`, `ruler_id`, `scout`, `infantry`, `cavalry`, `artillery`, `x`, `y`, `actual_x`, `actual_y` ) ' .
				'VALUES( :name, :officer_id, :ruler_id, :scout, :infantry, :cavalry, :artillery, :x, :y, :actual_x, :actual_y )' );
		$statement->bindParam( ':name', $armyName, PDO::PARAM_STR );
		$statement->bindParam( ':officer_id', $officerId, PDO::PARAM_INT );
		$statement->bindParam( ':ruler_id', $rulerId, PDO::PARAM_INT );
		$statement->bindParam( ':scout', $scouts, PDO::PARAM_INT );
		$statement->bindParam( ':infantry', $infantry, PDO::PARAM_INT );
		$statement->bindParam( ':cavalry', $cavalry, PDO::PARAM_INT );
		$statement->bindParam( ':artillery', $artillery, PDO::PARAM_INT );
		$statement->bindParam( ':x', $x, PDO::PARAM_INT );
		$statement->bindParam( ':y', $y, PDO::PARAM_INT );
		$statement->bindParam( ':actual_x', $x, PDO::PARAM_INT );
		$statement->bindParam( ':actual_y', $y, PDO::PARAM_INT );
		$dal->execute( $statement );

		return true;
	}
	else {
		return false;
	}
}
?>
